package com.waimai.api.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sun.org.apache.bcel.internal.generic.NEW;
import com.waimai.api.model.ResponseModel;
import com.waimai.db.entity.Comment;
import com.waimai.db.entity.Food;
import com.waimai.db.entity.Order;
import com.waimai.db.entity.ReceiveAddress;
import com.waimai.db.entity.Restaurant;
import com.waimai.db.entity.User;
import com.waimai.db.service.RestaurantService;
import com.waimai.db.service.UserService;
import com.waimai.util.SystemUtil;

@Controller
public class UserController {
	
	@Autowired
	public UserService userService;
	
	@ResponseBody
	@RequestMapping(value="/login", method=RequestMethod.POST, consumes="application/json")
	public ResponseModel<User> login(@RequestBody User user){
		User data = null;
		int statusCode = ResponseModel.STATUS_SERVER_ERROR;
		try {
			data = userService.checkUser(user);
			if(data != null){
				statusCode = ResponseModel.STATUS_LOGIN_SUCCESS;
			}else{
				data = user;
				statusCode = ResponseModel.STATUS_LOGIN_FAILED;
			}
		} catch (Exception e) {
			e.printStackTrace();
			data = null;
			statusCode = ResponseModel.STATUS_SERVER_ERROR;
		}
		
		return new ResponseModel<User>(data, statusCode); 
	}
	
	@ResponseBody
	@RequestMapping(value="/regist", method=RequestMethod.POST, consumes="application/json")
	public ResponseModel<User> regist(@RequestBody User user){
		User data = null;
		int statusCode = ResponseModel.STATUS_SERVER_ERROR;
		try {
			if(user != null && user.getPassword() == null && user.getPhone() != null){
				data = userService.checkPhone(user.getPhone());
				if(data == null){
					statusCode = ResponseModel.STATUS_REGIST_PHONE_AVALIABLE;
					data = user;
				}else{
					statusCode = ResponseModel.STATUS_REGIST_PHONE_EXTIST;
				}
			}else{
				data = userService.addUser(user);
				if(data == null){
					statusCode = ResponseModel.STATUS_REGIST_FAILED;
					data = user;
				}else{
					statusCode = ResponseModel.STATUS_REGIST_SUCCESS;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			statusCode = ResponseModel.STATUS_SERVER_ERROR;
			data = null;
		}
		
		return new ResponseModel<User>(data, statusCode); 
	}
	
	@ResponseBody
	@RequestMapping(value="/user/{userId}", method=RequestMethod.PUT, consumes="application/json")
	public ResponseModel<User> updateUser(@PathVariable("userId") Integer userId,
			@RequestBody User user){
		User data = null;
		int statusCode = ResponseModel.STATUS_SERVER_ERROR;
		try {
			data = userService.updateUser(user);
			if(data == null){
				statusCode = ResponseModel.STATUS_SERVER_ERROR;
				data = user;
			}else{
				statusCode = ResponseModel.STATUS_REQUEST_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			statusCode = ResponseModel.STATUS_SERVER_ERROR;
			data = null;
		}
		
		return new ResponseModel<User>(data, statusCode);
	}
	
	@ResponseBody
	@RequestMapping(value="/user/{userId}/addresses", method=RequestMethod.GET)
	public ResponseModel<List<ReceiveAddress>> addresses(@PathVariable("userId") Integer userId){
		List<ReceiveAddress> data = null;
		int statusCode = ResponseModel.STATUS_SERVER_ERROR;
		try {
			data = userService.getAddresses(userId);
			if(data.size() <= 0){
				statusCode = ResponseModel.STATUS_GET_DATA_NONE;
			}else{
				statusCode = ResponseModel.STATUS_GET_DATA_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			statusCode = ResponseModel.STATUS_SERVER_ERROR;
			data = null;
		}
			
		return new ResponseModel<List<ReceiveAddress>>(data, statusCode);
	}
	
	@ResponseBody
	@RequestMapping(value="/user/{userId}/address", method=RequestMethod.POST, consumes="application/json")
	public ResponseModel<ReceiveAddress> addAddress(@PathVariable("userId") Integer userId,
			@RequestBody ReceiveAddress address){
		ReceiveAddress data = null;
		int statusCode = ResponseModel.STATUS_SERVER_ERROR;
		try {
			data = userService.addAddress(address);
			if(data == null){
				statusCode = ResponseModel.STATUS_REQUEST_FAILED;
			}else{
				statusCode = ResponseModel.STATUS_REQUEST_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			statusCode = ResponseModel.STATUS_SERVER_ERROR;
			data = null;
		}
			
		return new ResponseModel<ReceiveAddress>(data, statusCode);
	}
	
	@ResponseBody
	@RequestMapping(value="/user/{userId}/address/{addressId}", method=RequestMethod.PUT, consumes="application/json")
	public ResponseModel<ReceiveAddress> updateAddress(@PathVariable("userId") Integer userId,
			@PathVariable("addressId") Integer addressId,
			@RequestBody ReceiveAddress address){
		ReceiveAddress data = null;
		int statusCode = ResponseModel.STATUS_SERVER_ERROR;
		try {
			data = userService.updateAddress(address);
			if(data == null){
				statusCode = ResponseModel.STATUS_REQUEST_FAILED;
			}else{
				statusCode = ResponseModel.STATUS_REQUEST_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			statusCode = ResponseModel.STATUS_SERVER_ERROR;
			data = null;
		}
			
		return new ResponseModel<ReceiveAddress>(data, statusCode);
	}
	
	@ResponseBody
	@RequestMapping(value="/user/{userId}/address/{addressId}", method=RequestMethod.DELETE)
	public ResponseModel<ReceiveAddress> deleteAddress(@PathVariable("userId") Integer userId,
			@PathVariable("addressId") Integer addressId){
		ReceiveAddress data = null;
		int statusCode = ResponseModel.STATUS_SERVER_ERROR;
		try {
			data = userService.deleteAddress(addressId);
			if(data == null){
				statusCode = ResponseModel.STATUS_REQUEST_FAILED;
			}else{
				statusCode = ResponseModel.STATUS_REQUEST_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			statusCode = ResponseModel.STATUS_SERVER_ERROR;
			data = null;
		}
			
		return new ResponseModel<ReceiveAddress>(data, statusCode);
	}
	
	@ResponseBody
	@RequestMapping(value="/user/{userId}/orders", method=RequestMethod.GET)
	public ResponseModel<List<Order>> orders(@PathVariable("userId") Integer userId,
			HttpServletRequest request){
		List<Order> data = null;
		int statusCode = ResponseModel.STATUS_SERVER_ERROR;
		try {
			data = userService.getOrders(userId);
			if(data != null){
				for(int i=0;i<data.size();i++){
					String photoName = data.get(0).getRestaurant().getPhoto();
					if(photoName != null && !"".equals(photoName)){
						data.get(i).getRestaurant().setPhoto(SystemUtil.getPhotoUrl(request, photoName));
					}
				}
				if(data.size() == 0){
					statusCode = ResponseModel.STATUS_GET_DATA_NONE;
				}else{
					statusCode = ResponseModel.STATUS_GET_DATA_SUCCESS;
				}
			}else{
				statusCode = ResponseModel.STATUS_SERVER_ERROR;
			}
		} catch (Exception e) {
			e.printStackTrace();
			statusCode = ResponseModel.STATUS_SERVER_ERROR;
			data = null;
		}
			
		return new ResponseModel<List<Order>>(data, statusCode);
	}
	
	@ResponseBody
	@RequestMapping(value="/user/{userId}/order/{orderId}", method=RequestMethod.GET)
	public ResponseModel<Order> order(@PathVariable("userId") Integer userId,
			@PathVariable("orderId") Integer orderId){
		Order data = null;
		int statusCode = ResponseModel.STATUS_SERVER_ERROR;
		try {
			data = userService.getOrder(orderId);
			if(data != null){
				statusCode = ResponseModel.STATUS_GET_DATA_SUCCESS;
			}else{
				statusCode = ResponseModel.STATUS_GET_DATA_NONE;
			}
		} catch (Exception e) {
			e.printStackTrace();
			statusCode = ResponseModel.STATUS_SERVER_ERROR;
			data = null;
		}
			
		return new ResponseModel<Order>(data, statusCode);
	}
	
	@ResponseBody
	@RequestMapping(value="/user/{userId}/order/{orderId}/comment", method=RequestMethod.POST, consumes="application/json")
	public ResponseModel<Comment> addComment(@PathVariable("userId") Integer userId,
			@PathVariable("orderId") Integer orderId,
			@RequestBody Comment comment){
		Comment data = null;
		int statusCode = ResponseModel.STATUS_SERVER_ERROR;
		try {
			data = userService.addComment(orderId, comment);
			if(data != null){
				statusCode = ResponseModel.STATUS_REQUEST_SUCCESS;
			}else{
				statusCode = ResponseModel.STATUS_REQUEST_FAILED;
			}
		} catch (Exception e) {
			e.printStackTrace();
			statusCode = ResponseModel.STATUS_SERVER_ERROR;
			data = null;
		}
			
		return new ResponseModel<Comment>(data, statusCode);
	}
}
